package com.etime.shuhuiyuan.services.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shuhuiyuan.dao.UserDao;
import com.etime.shuhuiyuan.pojo.User;
import com.etime.shuhuiyuan.services.user.interfaces.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * user业务逻辑的接口实现
 */
@Service
public class UserServiceImpl implements UserService {
    /**
     * 使用springboot的ioc容器自动注入实例
     */
    @Autowired
    private UserDao userDao;

    /**
     * 添加user
     * @param user
     * @return
     */
    @Override
    public int insertUser(User user) {
        return userDao.insert(user);
    }

    /**
     * 修改user
     * @param user
     * @return
     */
    @Override
    public int updataUser(User user) {
        return userDao.updateById(user);
    }

    /**
     * 查询user列表
     * @param user
     * @return
     */
    @Override
    public List<User> selectUserList(User user) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        if(user!=null && user.getUsername()!=null && !user.getUsername().equalsIgnoreCase("")){
            wrapper.eq("username",user.getUsername());
        }
        if(user!=null && user.getUserstate()!=null && user.getUserstate()!=-1){
            wrapper.eq("userstate",user.getUserstate());
        }
        return userDao.selectList(wrapper);
    }

    /**
     * 分页查询
     * @param user
     * @param pagenum
     * @param pagesize
     * @return
     */
    @Override
    public Page<User> selectUserByPage(User user, long pagenum, long pagesize) {
        //分页参数
        Page<User> page=new Page<>(pagenum,pagesize);
        //查询操作
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        if(user!=null && user.getUsername()!=null && !user.getUsername().equalsIgnoreCase("")){
            wrapper.like("username",user.getUsername());
        }
        if(user!=null && user.getUserstate()!=null && user.getUserstate()!=-1){
            wrapper.eq("userstate",user.getUserstate());
        }
        return userDao.selectPage(page,wrapper);
    }

    /**
     * 根据id查询user
     * @param userid
     * @return
     */
    @Override
    public User selectUserById(Integer userid) {
        return userDao.selectById(userid);
    }
}